package com.gz.steak.modules.system.service.impl;

import com.gz.steak.modules.system.mapper.SysDeptMapper;
import com.gz.steak.modules.system.pojo.dataobject.SysDept;
import com.gz.steak.modules.system.pojo.vo.SysDeptVo;
import com.gz.steak.utils.convert.ConvertUtil;
import com.gz.steak.modules.system.service.SysDeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 刘牌
 * @Date 2020/3/20 0020 20:11
 * @Version 1.0
 */
@Service
public class SysDeptServiceImpl implements SysDeptService {
    private final static Logger logger = LoggerFactory.getLogger(SysDeptServiceImpl.class);

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    public List<SysDept> get() {
        List<SysDept> sysDepts = new ArrayList<>();
        List<SysDept> depts = sysDeptMapper.get();
        for (SysDept dept : depts) {
            if (dept.getDeptParentId() == null || dept.getDeptParentId() == 0) {
                dept.setLevel(0);
                sysDepts.add(dept);
            }
        }
        findChildren(sysDepts, depts);
        return sysDepts;
    }

    @Override
    public void save(SysDept sysDept) {
        sysDept.setCreateTime(ConvertUtil.FormatDate(new Date()));
        int i = sysDeptMapper.insertSelective(sysDept);
    }

    /**
     * 删除部门
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //判断是否有下级部门
        int i = sysDeptMapper.getCountById(id);
        if (i == 0){
            sysDeptMapper.deleteByPrimaryKey(id); //没有下级部门，只删除本身

        }else {
            sysDeptMapper.deleteByParentId(id); //有下级部门，删除本身和下级部门
        }
    }

    /**
     * 修改用户
     * @param sysDeptVo
     */
    @Override
    public void edit(SysDeptVo sysDeptVo) {
        SysDept sysDept = new SysDept();
        sysDept.setId(sysDeptVo.getId())
                .setDeptName(sysDeptVo.getDeptName())
                .setDeptParentId(sysDeptVo.getDeptParentId())
                .setDeptLeader(sysDeptVo.getDeptLeader())
                .setDeptLeaderPhone(sysDeptVo.getDeptLeaderPhone())
                .setDeptOrderNum(sysDeptVo.getDeptOrderNum())
                .setLastUpdateTime(ConvertUtil.FormatDate(new Date()));
        logger.info("d  "+sysDept);
        int i = sysDeptMapper.updateByPrimaryKeySelective(sysDept);
        logger.info("i   "+i);
    }

    private void findChildren(List<SysDept> sysDepts, List<SysDept> depts) {
        for (SysDept sysDept : sysDepts) {
            List<SysDept> children = new ArrayList<>();
            for (SysDept dept : depts) {
                if (sysDept.getId() != null && sysDept.getId().equals(dept.getDeptParentId())) {
                    dept.setDeptParentName(sysDept.getDeptName());
                    dept.setLevel(sysDept.getLevel() + 1);
                    children.add(dept);
                }
            }
            sysDept.setChildren(children);
            findChildren(children, depts);
        }
    }

}
